/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.data.controller.DistributionLnCtlr;
import org.gov.vis.data.controller.DistributionLnCtlrImpl;
import org.gov.vis.data.dto.DistributionLnDto;
import org.gov.vis.data.gui.DistributionLNDialog;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author user
 *
 */
public class DistributionLNListener implements ActionListener,BaseListener,FocusListener {


	private DistributionLNDialog distributionLnDailog;
	private DistributionLnCtlr distributionLnCtlr = new DistributionLnCtlrImpl();
	private ErrorsDisplayJPanel errorMsgPanel;
	final private ResourceBundle rb = Util.getResources();
	private DistributionLnDto distributionLnDto;

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */

	public DistributionLNListener(DistributionLNDialog distributionLnDailog){

		this.distributionLnDailog = distributionLnDailog;
		
	}

	public void actionPerformed(ActionEvent actionEvent) {
		try{
			errorMsgPanel = new ErrorsDisplayJPanel(distributionLnDailog.getjPanel1());
			errorMsgPanel.clearMsgPanel();

			if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_save"))
					|| actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){
				//Method called to either Add or Edit the GopalaMithra details
				addOrEditDistributionLN();
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_view"))){	
				//Method called to view the details of DistributionLN Object based on the passed distributionLnId
				viewDistributionLN(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
				//Method called to delete the DistributionLN Object based on the passed distributionLnId.
				deleteDistributionLN(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_reset"))){
				//Method called to clear the screen data
				clear();
			}
			/*else if(actionEvent.getActionCommand().equalsIgnoreCase("txtFldDLNKgs")){
				convertKgsToLitters();
			}*/
		}catch(Exception e){
			e.printStackTrace();
		}
	}//end actionPerformed


	private void convertKgsToLitters() {
		// TODO Auto-generated method stub
		HashMap<String, String> errorMsgs = new HashMap<String, String>();
		List<String> errorMsgLst = new ArrayList<String>();
		try{
			String _disLNKgsErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_dist_ln_kgs"),distributionLnDailog.getTxtFldDLNKgs().getText() , ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(_disLNKgsErr != null)
				errorMsgs.put(rb.getString("dialog_label_dist_ln_kgs").toUpperCase(), _disLNKgsErr);
			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();
			double kgs = Double.parseDouble(distributionLnDailog.getTxtFldDLNKgs().getText());
			double litters = kgs * 1.078;
			distributionLnDailog.getTxtFldDLNConvertedLtrs().setText(String.valueOf(litters));


		}catch(InvalidInputException iie){
			String errMsg = null;
			if(errorMsgs.containsKey(rb.getString("dialog_label_dist_ln_kgs").toUpperCase())){
				distributionLnDailog.getLblKgs().setForeground(redColor);
				errMsg = errorMsgs.remove(rb.getString("dialog_label_dist_ln_kgs").toUpperCase());
				errorMsgLst.add(errMsg);
			}

			errorMsgPanel.setErrorMessages(errorMsgLst);

		}
	}


	private void viewDistributionLN(long _distributionLNId) {
		// TODO Auto-generated method stub

		try{
			final DistributionLnDto _distributionLnDto = distributionLnCtlr.getDisLN(_distributionLNId);
			setDtoDetailsToGui(_distributionLnDto);
			distributionLnDailog.validate();
			distributionLnDailog.repaint();
		}catch(Exception e){
			e.printStackTrace();
		}
	}//end viewDistributionLN

	/**
	 * Method called to either Add or Edit the GopalaMithra details
	 */
	private void addOrEditDistributionLN(){
		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(distributionLnDailog);

		try{
			//Method called to Add or Edit the GopalaMithra
			distributionLnCtlr.addOrEditDisLN(distributionLnDto);

			/*List<String> successMsgLst = new ArrayList<String>();
			successMsgLst.add(rb.getString(GuiConstants.DATA_ADD_DISTRBUTIONLN_SUCCESS));
			errorMsgPanel.setSuccessMessages(successMsgLst);
			*/
			JOptionPane.showMessageDialog(distributionLnDailog, rb.getString(GuiConstants.DATA_ADD_DISTRBUTIONLN_SUCCESS));
			//distributionLnDailog.dispose();
			

			//Method called to clear the screen data
			clear();
		}catch(Exception e){
			List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = DistributionLnCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;
					if(_exceptions.containsKey(rb.getString("dialog_label_dist_ln_code").toUpperCase())){
						distributionLnDailog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_ln_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_dist_ln_dtofdstr").toUpperCase())){
						distributionLnDailog.getLblDateOfDistribution().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_ln_dtofdstr").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_dist_ln_frmstation").toUpperCase())){
						distributionLnDailog.getLblFromStation().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_ln_frmstation").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_dist_ln_vehiceno").toUpperCase())){
						distributionLnDailog.getLblVehicalNo().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_ln_vehiceno").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_dist_ln_kgs").toUpperCase())){
						distributionLnDailog.getLblKgs().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_ln_kgs").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					errorMsgPanel.setErrorMessages(errorMsgLst);
				} else {
					//log.debug(e.getMessage(), e);
				}	
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);

			}
		}		
	}//end addOrEditGopalaMithra

	/**
	 * Method called to delete the DistributionLN Object based on the passed distributionLnId
	 * @param _distributionLnId
	 */
	private void deleteDistributionLN(final long  _distributionLnId){
		try{
			distributionLnCtlr.deleteDisLN(_distributionLnId);
		}catch(Exception e){
			e.getMessage();
		}
	}//end deleteDistributionLN


	/**
	 * Method called to Clear all the form fields of the screen
	 */
	public void clear() {
		//set the Field's data to empty values
		distributionLnDailog.getTxtFldCode().setText("");
		distributionLnDailog.getTxtFldDLNConvertedLtrs().setText("");
		distributionLnDailog.getDtChoDateOfDistribution().setDate(null);
		distributionLnDailog.getTxtFldDLNEvoporation().setText("");
		distributionLnDailog.getTxtFldDLNFromStation().setText("");
		distributionLnDailog.getTxtFldDLNKgs().setText("");
		distributionLnDailog.getTxtFldDLNVehicalNo().setText("");

		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);


	}//end clear

	/**
	 * Method called to read the data from GUI and set them to DTO Object 
	 * @param _guiObject
	 */
	@Override
	public void setDtoDetailsToGui(Object object) {
		
		DistributionLnDto _distributionLnDto = (DistributionLnDto)object;
		distributionLnDailog.getTxtFldCode().setText(_distributionLnDto.getCode());
		distributionLnDailog.getTxtFldDLNConvertedLtrs().setText(_distributionLnDto.getCnvtdLtrs());
		distributionLnDailog.getTxtFldDLNDODistribution().setText(DU.format(_distributionLnDto.getDoDstrbut(),DBConstants.DISPLAY_DATE_FORMAT));
		distributionLnDailog.getTxtFldDLNEvoporation().setText(_distributionLnDto.getEvopLoss());
		distributionLnDailog.getTxtFldDLNFromStation().setText(_distributionLnDto.getFrmStation());
		distributionLnDailog.getTxtFldDLNKgs().setText(_distributionLnDto.getKgs());
		distributionLnDailog.getTxtFldDLNVehicalNo().setText(_distributionLnDto.getVehicleNo());


	}

	/**
	 * Method called to read the data from DTO and set them to GUI.
	 * @param _guiObject
	 */
	@Override
	public void setGuiDetailsToDto(Object object) {
		
		DistributionLNDialog _distributionLNDialog = (DistributionLNDialog)object;
		distributionLnDto = new DistributionLnDto();
		distributionLnDto.setCode(_distributionLNDialog.getTxtFldCode().getText());
		distributionLnDto.setCnvtdLtrs(_distributionLNDialog.getTxtFldDLNConvertedLtrs().getText());
		//distributionLnDto.setDoDstrbut(DU.parse(_distributionLNDialog.getTxtFldDLNDODistribution().getText()));
		if(_distributionLNDialog.getDtChoDateOfDistribution().getDate() != null){
			distributionLnDto.setDoDstrbut(_distributionLNDialog.getDtChoDateOfDistribution().getDate());
		}

		distributionLnDto.setEvopLoss(_distributionLNDialog.getTxtFldDLNEvoporation().getText());
		distributionLnDto.setFrmStation(_distributionLNDialog.getTxtFldDLNFromStation().getText());
		distributionLnDto.setKgs(_distributionLNDialog.getTxtFldDLNKgs().getText());
		distributionLnDto.setVehicleNo(_distributionLNDialog.getTxtFldDLNVehicalNo().getText());

	}

	public void setDistributionLNsDailog(final DistributionLNDialog _distrbutionLNDailog) {
		this.distributionLnDailog = _distrbutionLNDailog;
	}

	public DistributionLNDialog getGopalaMithrasDailog() {
		return distributionLnDailog;
	}

	@Override
	public void clearGUILabelColors(Color _lblColor) {
		//set the Field's Labels to default Black color
		distributionLnDailog.getLblCode().setForeground(_lblColor);
		distributionLnDailog.getLblConvetedLtrs().setForeground(_lblColor);
		distributionLnDailog.getLblDateOfDistribution().setForeground(_lblColor);
		distributionLnDailog.getLblEvoporation().setForeground(_lblColor);
		distributionLnDailog.getLblFromStation().setForeground(_lblColor);
		distributionLnDailog.getLblKgs().setForeground(_lblColor);
		distributionLnDailog.getLblVehicalNo().setForeground(_lblColor);


	}

	@Override
	public void focusGained(FocusEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		// TODO Auto-generated method stub
		errorMsgPanel = new ErrorsDisplayJPanel(distributionLnDailog.getjPanel1());
		errorMsgPanel.clearMsgPanel();
		clearGUILabelColors(blackColor);
		
		convertKgsToLitters();
	}	


}//end class
